package com.test.daily.leetcode.y2022.m07.day0714.v02;

import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * @descriptions:
 * @author: Tom
 * @date: 2022/7/14 上午 10:49
 * @version: 1.0
 */
public class Solution {
    public static int maxCover1(int[][] lines){
        int min = Integer.MAX_VALUE;
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < lines.length; i++) {
            min = Math.min(min, lines[i][0]);
            max = Math.max(max, lines[i][1]);
        }
        int ans = 0;
        for(double p = min + 0.5; p < max; p+=1){
            int cur = 0;
            for(int j = 0; j < lines.length; j++){
                if(lines[j][0] < p && lines[j][1] > p){
                    cur++;
                }
            }
            ans = Math.max(ans, cur);
        }
        return ans;
    }



    public static class Line{
        int start;
        int end;
        public Line(int s, int e){
            this.start = s;
            this.end = e;
        }
    }
    public static class MyLineComparator implements Comparator<Line>{
        @Override
        public int compare(Line o1, Line o2) {
            return o1.end - o2.end;
        }
    }
    public static int coverMax2(int[][] lines){
        Line[] lineCs = new Line[lines.length];
        for (int i = 0; i < lines.length; i++) {
            lineCs[i] = new Line(lines[i][0], lines[i][1]);
        }
        Arrays.sort(lineCs, new MyLineComparator());
        PriorityQueue<Integer> pq = new PriorityQueue<>();
        int ans = 0;
        for (int i = 0; i < lines.length; i++) {
            while(pq.size() != 0 && pq.peek() < lineCs[i].start){
                pq.poll();
            }
            pq.add(lineCs[i].end);
            ans = Math.max(ans, pq.size());
        }
        return ans;
    }
}
